/**
 * Manages the database creation and connections. 
 * 
 * @author Alican Ozgoren
 * @version 12/14/2012
 */

package edu.brockport.shuttleapp.database;

import edu.brockport.shuttleapp.Utilities;
import edu.brockport.shuttleapp.exception.UnexpectedProblemException;

import java.io.IOException;
import java.util.Properties;
import java.util.Vector;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class DatabaseManager
{
	private static DatabaseHelper myDBHelper = null; 
	protected Context context = null;
	
	//----------------------------------------------------------
	public DatabaseManager(Context context) throws UnexpectedProblemException
	{
		this.context = context;
		establishDBConnection();
	}
	
	// Establish database connection
	//----------------------------------------------------------
    private void establishDBConnection() throws UnexpectedProblemException
    {	 
    	myDBHelper = new DatabaseHelper(context);
    	
        try 
        {
        	myDBHelper.createDataBase();
        	
        	//Utilities.toaster(context, "Successfully connected to the database!");
        } 
        catch (IOException ioe) 
        {
        	throw new UnexpectedProblemException("Database could not be created.");
 
        }
        catch(SQLException sqle)
        {
        	throw new UnexpectedProblemException("Database could not be opened.");
        }
    }
    
    //----------------------------------------------------------
    public static DatabaseHelper getDBHelper()
    {
    	return myDBHelper;
    }
    
    //----------------------------------------------------------
    protected Vector getSelectQueryResult(String sqlSelectStatement)
    {
		int numColumns = 0; 			
		Vector namesColumns = null;
		SQLiteDatabase myDB = null;

		try
		{
			// Open the database to execute the query
			myDBHelper.open();
			
		    myDB = myDBHelper.getDatabase();
	        Cursor cursor = myDB.rawQuery(sqlSelectStatement, null);

			// verify the results
			if(cursor.getCount() == 0)
			{
				// Utilities.toaster(context, "No result!"); // DEBUG
				cursor.close();
				myDB.close();
				myDBHelper.close();
				
				return null;
			}

			numColumns = cursor.getColumnCount();
			namesColumns = new Vector();
			for(int cnt = 0; cnt < numColumns; cnt++)
			{
				String thisColumnName = cursor.getColumnName(cnt);
				namesColumns.addElement(thisColumnName);
			}

			Vector cursorToReturn = new Vector();
			
			if(cursor.moveToFirst()) 
	        {
	            do 
	            {
	            	Properties thisRow = new Properties();
					for (int cnt = 0; cnt < numColumns; cnt++)
					{
						String theColumnName = (String) namesColumns.elementAt(cnt);
						String theColumnValue = cursor.getString(cnt);

						if (theColumnValue != null)
						{
							thisRow.setProperty(theColumnName, theColumnValue);
						}
					}
					cursorToReturn.addElement(thisRow);
	            } while (cursor.moveToNext());
	        }

			if(cursor != null)
				cursor.close();
			
			myDB.close();
			myDBHelper.close();
			
			return cursorToReturn;
		}
		catch (SQLException sqle)
		{
			Utilities.toaster(context, "Unexpected database problem!");
			return null;
		}
	}

}
